package ulity;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;


import model.UserGen;

public class ParserXml {
  static final String USER = "user";
  static final String USERNAME = "username";
  static final String PASS = "pass";
  static final String EMAIL = "email";
 

  @SuppressWarnings({ "unchecked", "null" })
  public static List<UserGen> readUser(String configFile) {
    List<UserGen> users = new ArrayList<UserGen>();
    try {
      // First create a new XMLInputFactory
      XMLInputFactory inputFactory = XMLInputFactory.newInstance();
      // Setup a new eventReader
      InputStream in = new FileInputStream(configFile);
      XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
      // Read the XML document
      UserGen user = null;

      while (eventReader.hasNext()) {
        XMLEvent event = eventReader.nextEvent();

        if (event.isStartElement()) {
          StartElement startElement = event.asStartElement();
          // If we have a item element we create a new item
          if (startElement.getName().getLocalPart() == (USER)) {
            user = new UserGen();
            // We read the attributes from this tag and add the date
            // attribute to our object
            
          }

          if (event.isStartElement()) {
            if (event.asStartElement().getName().getLocalPart()
                .equals(USERNAME)) {
              event = eventReader.nextEvent();
              user.setUserName(event.asCharacters().getData());
              continue;
            }
          }
          if (event.asStartElement().getName().getLocalPart()
              .equals(PASS)) {
            event = eventReader.nextEvent();
            user.setPass(event.asCharacters().getData());
            continue;
          }

          if (event.asStartElement().getName().getLocalPart()
              .equals(EMAIL)) {
            event = eventReader.nextEvent();
            user.setEmail(event.asCharacters().getData());
            continue;
          }

        
        }
        // If we reach the end of an item element we add it to the list
        if (event.isEndElement()) {
          EndElement endElement = event.asEndElement();
          if (endElement.getName().getLocalPart() == (USER)) {
            users.add(user);
          }
        }

      }
    } catch (FileNotFoundException e) {
      e.printStackTrace();
    } catch (XMLStreamException e) {
      e.printStackTrace();
    }
    return users;
  }
  /**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 ParserXml read = new ParserXml();
		    List<UserGen> readConfig = read.readUser("data/user2.xml");
		    for (UserGen user : readConfig) {
		      System.out.println(user);
		    }
	}
} 